/*
 * CoachFactBase.
 */
package robocup.component.coach;

import java.util.Vector;

import robocup.component.DAInamiteConstants;
import robocup.component.infotypes.AuralInfo;
import robocup.component.infotypes.CoachVisualInfo;

/**
 * CoachFactBase is a database with unmodified informations from the world.
 * 
 * @author rw
 */
public class CoachFactBase {

  private static final boolean          DEBUG         = false;

  private final Vector<CoachVisualInfo> visualHistory = new Vector<CoachVisualInfo>(
                                                    DAInamiteConstants.FB_HIST_SIZE_C);

  private final Vector<AuralInfo>       auralHistory  = new Vector<AuralInfo>(
                                                    DAInamiteConstants.FB_HIST_SIZE_C);

  private CoachVisualInfo         currentCVinfo = null;                                // Current
                                                                                        // CoachVisualInfo

  private AuralInfo               currentAinfo  = null;                                // Current
                                                                                        // AuralInfo

  /**
   * Returns the current CoachVisualInfo
   * 
   * @return current CoachVisualInfo
   */
  public CoachVisualInfo getCurrentVI() {

    return this.currentCVinfo;
  }

  /**
   * <code>getCurrentAinfo</code> getter
   * 
   * @return Returns the currentAinfo.
   */
  public AuralInfo getCurrentAI() {

    return this.currentAinfo;
  }

  /**
   * <code>update</code> backups old CoachVisualInfo
   * 
   * @param info
   */
  public void update(final CoachVisualInfo info) {

    if (info != null) {
      if (CoachFactBase.DEBUG) {
        System.out.println("CoachVisualInfo cycle: " + info.getCycle());
      }
      // Remove the last element if the CoachVisualInfo history is full
      if (DAInamiteConstants.FB_HIST_SIZE_C <= this.visualHistory.size()) {
        this.visualHistory.remove(this.visualHistory.lastElement());
      }

      // Backup old CoachVisualInfo
      this.visualHistory.add(0, this.currentCVinfo);
      // Update current CoachVisualInfo
      this.currentCVinfo = info;
    } // end if
  }

  /**
   * <code>update</code> updates the factbase after the arrival of an
   * AuralInfo. The old info object is put to the history.
   * 
   * @param info
   *          new info
   */
  public void update(final AuralInfo info) {

    if (info != null) {
      if (CoachFactBase.DEBUG) {
        System.out.println("AuralInfo cycle: " + info.getCycle());
      }
      // Remove the last element if the CoachVisualInfo history is full
      if (DAInamiteConstants.FB_HIST_SIZE_C <= this.auralHistory.size()) {
        this.auralHistory.remove(this.auralHistory.lastElement());
      }

      // Backup old CoachVisualInfo
      this.auralHistory.add(0, this.currentAinfo);
      // Update current CoachVisualInfo
      this.currentAinfo = info;
    }
  }

  /**
   * 
   * get old AuralInfos, if information about requested info no longer exists
   * the oldest available is returned
   * 
   * @param n
   *          steps back in time
   * @return
   */
  public AuralInfo getPreviousAI(final int n) {

    if (n == 0) {
      return this.currentAinfo;
    }
    if (n > this.auralHistory.size()) {
      return this.auralHistory.lastElement();
    }
    return this.auralHistory.elementAt(n - 1);
  }

  /**
   * 
   * get old VisualInfos, if information about requested info no longer exists
   * the oldest available is returned
   * 
   * @param n
   *          steps back in time
   * @return
   */
  public CoachVisualInfo getPreviousVI(final int n) {

    if (n == 0) {
      return this.currentCVinfo;
    }
    if (n > this.visualHistory.size()) {
      return this.visualHistory.lastElement();
    }
    return this.visualHistory.elementAt(n - 1);
  }
}

/**
 * Revision 1.0 2005/01/08 14:34:00 ullihtch Added tag for CVS history
 * 
 */
